﻿@inject IDatabaseService DatabaseService

<MudDialog>
    <DialogContent>
        <MudForm @ref="_editRequestForm" Model="_editRequestDto" Validation="@(_editValidator.ValidateValue)" ValidationDelay="0">
            <MudGrid>
                <MudItem xs="6">
                    <MudSelect @bind-Value="_editRequestDto.DriverName" For="@(() => _editRequestDto.DriverName)" Label="数据库类型" Dense="true"
                               AnchorOrigin="Origin.BottomCenter" Variant="Variant.Outlined" Margin="Margin.Dense">
                        <MudSelectItem Value="@("mysql")">MySql</MudSelectItem>
                        <MudSelectItem Value="@("sqlServer")">SqlServer</MudSelectItem>
                        <MudSelectItem Value="@("oracle")">Oracle</MudSelectItem>
                    </MudSelect>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.DatabaseName" For="@(() => _editRequestDto.DatabaseName)" Label="数据库名称"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.HostUri" For="@(() => _editRequestDto.HostUri)" Label="主机"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>

                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Port" For="@(() => _editRequestDto.Port)" Label="端口"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>

                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.UserName" For="@(() => _editRequestDto.UserName)" Label="用户名"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>

                <MudItem xs="6">
                    <MudPasswordField @bind-Value="_editRequestDto.Password" For="@(() => _editRequestDto.Password)" Label="密码"
                                      Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudPasswordField>
                </MudItem>
                <MudItem xs="6">
                    <MudNumericField @bind-Value="_editRequestDto.Sort" For="@(() => _editRequestDto.Sort)" Label="排序"
                                     Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudNumericField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Charset" For="@(() => _editRequestDto.Charset)" Label="字符编码"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12">
                    <MudTextField @bind-Value="_editRequestDto.Description" For="@(() => _editRequestDto.Description)" Label="描述" Lines="3"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SubmitHandle">保存</MudButton>
        <MudButton Variant="Variant.Filled" OnClick="CancelHandle">取消</MudButton>
    </DialogActions>
</MudDialog>


@code {


    private DatabaseEditRequestDto _editRequestDto = new();

    private DatabaseEditValidator _editValidator = new();

    private MudForm _editRequestForm = new();

    [CascadingParameter]
    public MudDialogInstance? MudDialog { get; set; }

    [Parameter]
    public Guid? Key { get; set; }

    void CancelHandle()
    {
        if (MudDialog != null)
            MudDialog.Cancel();
    }

    async Task SubmitHandle()
    {
        await _editRequestForm.Validate();

        if (Key != null)
        {
            var result = await DatabaseService.UpdateDatabase(Key.Value, _editRequestDto);

            if (result.Succeeded && MudDialog != null)
            {
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
        else
        {
            var result = await DatabaseService.CreateDatabase(_editRequestDto);

            if (result.Succeeded && MudDialog != null)
            {
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new DatabaseEditRequestDto();
        if (Key != null)
        {
            var result = await DatabaseService.GetDatabase(Key.Value);
            if (result.Succeeded && result.Data != null)
            {
                _editRequestDto.DatabaseName = result.Data.DatabaseName;
                _editRequestDto.Charset = result.Data.Charset;
                _editRequestDto.Password = result.Data.Password;
                _editRequestDto.DriverName = result.Data.DriverName;
                _editRequestDto.Port = result.Data.Port;
                _editRequestDto.HostUri = result.Data.HostUri;
                _editRequestDto.UserName = result.Data.UserName;
                _editRequestDto.Sort = result.Data.Sort;
                _editRequestDto.Description = result.Data.Description;
            }
        }
    }

}